package com.maxd.modules.selfshop.util;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.profile.DefaultProfile;
import com.maxd.model.CommonInfo;
import com.maxd.respository.common.CommonRepository;
import com.maxd.utils.Config;
import com.maxd.utils.Md5Utils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * 短信工具
 */
@Slf4j
@Component
public class SmsUtil {


    private static CommonRepository commonRepository;

    @Autowired
    private CommonRepository getCommonRepository;

    @PostConstruct // 初始化
    public void init(){
        commonRepository=getCommonRepository;
    }

    /*
    模版名称:多商户申请通知
    模版CODE:
    SMS_205132750
    模版内容:
    您好，您的商户入驻资料已经提交，正在审核中。

    模版名称:多商户审核成功
    模版CODE:
    SMS_205821157
    模版内容:
    您好，您的商户资料审核成功，已为您注册账号，账号密码均为您的手机号，您可前往商户端登录操作。

    模版名称:多商户审核失败
    模版CODE:
    SMS_205821153
    模版内容:
    您好，您提交的商户资料审核失败，请您尽快核对信息或补充资料。
     */

    public static boolean send(String phone, String sendCode, String state) {
        CommonInfo one = commonRepository.findOne(79);
        boolean flag = true;
        if("2".equals(one.getValue())){
            CommonInfo accessKey = commonRepository.findOne(83);
            CommonInfo accessSecret = commonRepository.findOne(84);
            DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKey.getValue(), accessSecret.getValue());
            IAcsClient client = new DefaultAcsClient(profile);
            CommonRequest request = new CommonRequest();
            request.setDomain("dysmsapi.aliyuncs.com");
            request.setVersion("2017-05-25");
            request.setAction("SendSms");
            request.putQueryParameter("RegionId", "cn-hangzhou");
            request.putQueryParameter("PhoneNumbers", phone);
            request.putQueryParameter("SignName", commonRepository.findOne(95).getValue());

            String templateCode="";
            switch (state) {
                case "1":
                    templateCode = commonRepository.findOne(96).getValue();
                    break;
                case "2":
                    templateCode = commonRepository.findOne(97).getValue();
                    break;
                case "3":
                    templateCode = commonRepository.findOne(98).getValue();
                    break;
                default:
                    templateCode = commonRepository.findOne(113).getValue(); //多商户验证码
                    break;
            }
            request.putQueryParameter("TemplateCode", templateCode);
            if (StringUtils.isNotEmpty(sendCode)){
                request.putQueryParameter("TemplateParam", "{\"code\":\""+sendCode+"\"}");
            }
            try {
                CommonResponse response = client.getCommonResponse(request);
                System.out.println(response.getData());
            } catch (Exception e) {
                e.printStackTrace();
                flag = false;
            }
            return flag;
        }else{
            CommonInfo three = commonRepository.findOne(164);
            CommonInfo four = commonRepository.findOne(165);
            CommonInfo name = commonRepository.findOne(95);
            String testUsername = three.getValue(); //在短信宝注册的用户名
            String testPassword = four.getValue(); //在短信宝注册的密码
            String value="";
            switch (state) {
                case "1":
                    value = "【" + name.getValue() + "】您好，您的商户入驻资料已经提交，正在审核中。";
                    break;
                case "2":
                    value = "【" + name.getValue() + "】您好，您的商户资料审核成功，已为您注册账号，账号密码均为您的手机号，您可前往商户端登录操作。";
                    break;
                case "3":
                    value = "【" + name.getValue() + "】您好，您提交的商户资料审核失败，请您尽快核对信息或补充资料。";
                    break;
                default:
                    value = "【" + name.getValue() + "】您的商户验证码为: " + sendCode + "，10分钟内有效，如非您本人操作，可忽略本条消息";
                    break;
            }
            StringBuilder httpArg = new StringBuilder();
            httpArg.append("u=").append(testUsername).append("&");
            httpArg.append("p=").append(Md5Utils.md5s(testPassword)).append("&");
            httpArg.append("m=").append(phone).append("&");
            httpArg.append("c=").append(Md5Utils.encodeUrlString(value, "UTF-8"));
            String result = Md5Utils.request("https://api.smsbao.com/sms", httpArg.toString());
            log.error("短信包返回值："+result);
            if ("0".equals(result)) {
                return flag;
            }
            return false;
        }
    }

}
